package com.zmn.oms.api.controller.third.jddj;

import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.utils.RequestUtil;
import com.zmn.oms.third.jddj.JddjConsts;
import com.zmn.oms.third.jddj.dto.JddjRequestDTO;
import com.zmn.oms.third.jddj.dto.JddjResponseDTO;
import com.zmn.oms.third.jddj.dto.JddjTokenRequestDTO;
import com.zmn.oms.third.jddj.service.JddjService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：顺丰安装渠道对接API接口
 *
 * @author xiewenbing
 * @date 2021/01/18 17:00
 */

@RestController
@RequestMapping("/jddj")
@Slf4j
public class JddjController {

    @Autowired
    private JddjService jddjService;

    @PostMapping("/api/refreshToken")
    public JddjResponseDTO refreshToken(HttpServletRequest request, JddjTokenRequestDTO tokenRequestDTO) {

        JddjResponseDTO responseDTO = new JddjResponseDTO();

        if (Objects.isNull(tokenRequestDTO)) {
            responseDTO.setCode(String.valueOf(JddjConsts.RESPONSE_CODE_PARAM_DEFECT));
            responseDTO.setMsg("参数为空");
        }

        log.info("【{}】 刷新token请求 --【{}】",JddjConsts.CHANNEL_NAME,tokenRequestDTO.toString());
        
        String bodyContent = tokenRequestDTO.getToken();

        try {
            jddjService.handleRefreshToken(bodyContent);
            responseDTO.setCode(String.valueOf(JddjConsts.RESPONSE_CODE_SUCCESS));
            responseDTO.setMsg("success");

        } catch(OmsBaseException exception){
            responseDTO.setCode(String.valueOf(JddjConsts.RESPONSE_CODE_FAILED));
            responseDTO.setMsg(exception.getMessage());
        }

        log.info("【{}】 刷新token应答 --【{}】",JddjConsts.CHANNEL_NAME,responseDTO.toString());
        return responseDTO;
    }

    @PostMapping("/api/djsw/newOrder")
    public JddjResponseDTO createOrder(HttpServletRequest request, JddjRequestDTO requestDTO) {

        JddjResponseDTO responseDTO = new JddjResponseDTO();

        log.info("【{}】 收到创建订单请求 --【{}】",JddjConsts.CHANNEL_NAME,requestDTO);

        try {
            jddjService.handleCreateOrder(requestDTO);
            responseDTO.setCode(String.valueOf(JddjConsts.RESPONSE_CODE_SUCCESS));
            responseDTO.setMsg("success");

        } catch(OmsBaseException exception){
            responseDTO.setCode(String.valueOf(Optional.ofNullable(exception.getCode()).orElse(JddjConsts.RESPONSE_CODE_FAILED)));
            responseDTO.setMsg(exception.getMessage());
        }

        log.info("【{}】 创建订单应答 --【{}】",JddjConsts.CHANNEL_NAME,responseDTO.toString());
        return responseDTO;
    }

    @PostMapping("/api/djsw/venderAuditApplyCancelOrder")
    public JddjResponseDTO cancelOrder(HttpServletRequest request, JddjRequestDTO requestDTO) {

        JddjResponseDTO responseDTO = new JddjResponseDTO();

        log.info("【{}】 收到取消订单请求 --【{}】",JddjConsts.CHANNEL_NAME,requestDTO);

        try {
            jddjService.handleCancelOrder(requestDTO);
            responseDTO.setCode(String.valueOf(JddjConsts.RESPONSE_CODE_SUCCESS));
            responseDTO.setMsg("success");

        } catch(OmsBaseException exception){
            responseDTO.setCode(String.valueOf(Optional.ofNullable(exception.getCode()).orElse(JddjConsts.RESPONSE_CODE_FAILED)));
            responseDTO.setMsg(exception.getMessage());
        }

        log.info("【{}】 取消订单应答 --【{}】",JddjConsts.CHANNEL_NAME,responseDTO.toString());
        return responseDTO;
    }

    @PostMapping("/api/djsw/testNewOrder")
    public String testCreateOrder(String content, String sign) {
        if (!"TEST_SIGN_ZMN_666888".equals(sign)) {
            return "who are you";
        }

        log.info("【{}】 收到创建订单请求 --【{}】",JddjConsts.CHANNEL_NAME, content);

        try {
            Long workId = jddjService.createOrderByMsgContent(content);
            return String.format("orderId=[%d]", workId);
        } catch(OmsBaseException exception){
            return exception.getMessage();
        }
    }
}
